package NetModel.network;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;


import java.io.IOException;
import java.util.Map;

import NetModel.okhttp.requset.DeleteRequestBuilder;
import NetModel.okhttp.requset.GetRequestBuilder;
import NetModel.okhttp.requset.PostFileRequestBuilder;
import NetModel.okhttp.requset.PostRequestBuilder;
import NetModel.okhttp.requset.PostStringRequestBuilder;
import NetModel.okhttp.requset.PutByteRequestBuilder;
import NetModel.okhttp.requset.PutFileRequestBuilder;
import NetModel.okhttp.requset.PutRequestBuilder;


/**
 * Created by Administrator on 2016/10/26.
 * Okhttp实现网络基本操作方法
 */
public class NetOperationOkhttp implements INetOperation {

    @Override
    public Object get(@NonNull String url, @Nullable Map<String, String> heads, @Nullable Map<String, String> params, @Nullable Object tag) throws IOException, ClientException {
        okhttp3.Response response = new GetRequestBuilder()
                .url(url)
                .tag(tag)
                .heads(heads)
                .params(params)
                .build().execute();
        return response;
    }

    @Override
    public Object delete(@NonNull String url, @Nullable Map<String, String> heads, @Nullable Map<String, String> params, @Nullable Object tag) throws IOException, ClientException {
        okhttp3.Response response = new DeleteRequestBuilder()
                .url(url)
                .tag(tag)
                .heads(heads)
                .params(params)
                .build().execute();
        return response;
    }

    @Override
    public Object post(@NonNull String url, @Nullable Map<String, String> heads, @Nullable Map<String, String> params, @Nullable Object tag) throws IOException, ClientException {
        okhttp3.Response response = new PostRequestBuilder()
                .url(url)
                .tag(tag)
                .heads(heads)
                .params(params)
                .build().execute();
        return response;
    }

    @Override
    public Object postString(@NonNull String url, @Nullable Map<String, String> heads, @NonNull String context, @Nullable Object tag) throws IOException, ClientException {
        okhttp3.Response response = new PostStringRequestBuilder()
                .url(url)
                .context(context)
                .heads(heads)
                .tag(tag)
                .build().execute();
        return response;
    }

    @Override
    public Object postString(@NonNull String url, @Nullable Map<String, String> heads, @NonNull String context, @Nullable Object tag,
                             @NonNull Integer connectTimeout, @NonNull Integer readTimeout, @NonNull Integer writeTimeout) throws IOException, ClientException {
        okhttp3.Response response = new PostStringRequestBuilder()
                .url(url)
                .context(context)
                .heads(heads)
                .tag(tag)
                .connectTimeOut(connectTimeout)
                .readTimeOut(readTimeout)
                .writeTimeOut(writeTimeout)
                .build().execute();
        return response;
    }

    @Override
    public Object put(@NonNull String url, @Nullable Map<String, String> heads, @NonNull String context, @Nullable Object tag) throws IOException, ClientException {
        okhttp3.Response response = new PutRequestBuilder()
                .url(url)
                .context(context)
                .heads(heads)
                .tag(tag)
                .build().execute();
        return response;
    }

    @Override
    public Object postFile(@NonNull String url, @Nullable Map<String, String> heads, @NonNull String filePath, @Nullable Object tag) throws IOException, ClientException {
        okhttp3.Response response = new PostFileRequestBuilder()
                .url(url)
                .heads(heads)
                .file(filePath)
                .tag(tag)
                .build().execute();
        return response;
    }

    @Override
    public Object putFile(@NonNull String url, @Nullable Map<String, String> heads, @NonNull String filePath, @Nullable Object tag) throws IOException, ClientException {
        okhttp3.Response response = new PutFileRequestBuilder()
                .url(url)
                .heads(heads)
                .file(filePath)
                .tag(tag)
                .build().execute();
        return response;
    }

    @Override
    public Object putByte(@NonNull String url, @Nullable Map<String, String> heads, @NonNull byte[] bytes, @Nullable Integer offset, @Nullable Integer byteCount, @Nullable Object tag) throws IOException, ClientException {
        okhttp3.Response response = new PutByteRequestBuilder()
                .url(url)
                .heads(heads)
                .content(bytes, offset, byteCount)
                .tag(tag)
                .build().execute();
        return response;
    }


}
